<?php
if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class table_aljbd_type extends discuz_table{
	public function __construct() {

			$this->_table = 'aljbd_type';
			$this->_pk    = 'id';

			parent::__construct();
	}
    public function fetch_all_json()
    {
        $result = $this->fetch_all_by_upid_json(0);
        foreach ($result as $index => $item) {
            $result[$index]['name'] = diconv($item['name'], CHARSET, 'utf-8');
            $subregion = $this->fetch_all_by_upid_json($item['id']);
            if($subregion){
                foreach ($subregion as $subk => $subv){
                    $subregion[$subk]['name'] = diconv($subv['name'], CHARSET, 'utf-8');
                    $subsubregion = $this->fetch_all_by_upid_json($subv['id']);
                    if($subsubregion){
                        foreach($subsubregion as $subsubk => $subsubv){
                            $subsubregion[$subsubk]['name'] = diconv($subsubv['name'], CHARSET, 'utf-8');
                        }
                        $subregion[$subk]['sub'] = $subsubregion;
                    }else{
                        $subsubregion_1[0]['name'] = '--';
                        $subsubregion_1[0]['code'] = '0';
                        $subregion[$subk]['sub'] = $subsubregion_1;
                    }
                }
            }else{
                $subregion[0]['name'] = '--';
                $subregion[0]['code'] = '0';
                $subsubregion_1[0]['name'] = '--';
                $subsubregion_1[0]['code'] = '0';
                $subregion[0]['sub'] = $subsubregion_1;
            }
            $result[$index]['sub'] = $subregion;
        }
        return $result;
    }
    public function fetch_all_by_upid_json($upid){
        return DB::fetch_all('SELECT subject as name,upid,id,id as code FROM %t WHERE upid=%d ORDER BY displayorder ASC',array($this->_table,$upid));
    }
	public function fetch_upid_by_id($id=0){
		return DB::result_first('SELECT upid FROM %t WHERE id=%d',array($this->_table,$id));
	}
	public function fetch_subid_by_id($id=0){
		return DB::result_first('SELECT subid FROM %t WHERE id=%d',array($this->_table,$id));
	}
	/*public function fetch_all_by_upid($upid){
		return DB::fetch_all('SELECT * FROM %t WHERE upid=%d ORDER BY displayorder ASC',array($this->_table,$upid),'id');
	}*/
	public function fetch_all_by_upid($upid=0,$start=0,$perpage=0,$search=''){
		if($perpage){
			if($search){
				return DB::fetch_all('SELECT * FROM %t WHERE upid=%d and subject like %s ORDER BY displayorder ASC limit %d,%d',array($this->_table,$upid,$search,$start,$perpage),'id');
			}else{
				return DB::fetch_all('SELECT * FROM %t WHERE upid=%d ORDER BY displayorder ASC limit %d,%d',array($this->_table,$upid,$start,$perpage),'id');
			}
		}else{
			return DB::fetch_all('SELECT * FROM %t WHERE upid=%d ORDER BY displayorder ASC',array($this->_table,$upid),'id');
		}
	}
	public function count_by_upid($upid=0,$search=''){
		if($search){
			return DB::result_first('select count(*) from %t where upid=%d and subject like %s',array($this->_table,$upid,$search));
		}else{
			return DB::result_first('select count(*) from %t where upid=%d ',array($this->_table,$upid));
		}
	}
	public function fetch_all_by_type($type=0){
		return DB::fetch_all('select * from %t where id=%d ORDER BY displayorder ASC',array($this->_table,$type));
	}

}




?>